package com.freeheart.validation.constraintvalidators.in;


import org.springframework.core.Ordered;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 通过反射获取InCollection实例
 *
 * @author: EmperorHeart
 * @date: 2025/5/10 1:05
 * @description
 */
public class ReflectInCollectionSupplier implements InCollectionSupplier, Ordered {

    @Override
    public boolean support() {
        return true;
    }

    @Override
    public InCollection getInCollectionInstance(Class<? extends InCollection> clazz) {
        try {
            Constructor<? extends InCollection> declaredConstructor = clazz.getDeclaredConstructor();
            // 私有构造也可以产生
            declaredConstructor.setAccessible(true);
            return declaredConstructor.newInstance();
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                 NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int getOrder() {
        return -2;
    }
}
